package edu.yuhf.array;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;
import java.util.stream.Stream;

public class Array {

    private int[] src = {3,4,5,2,4,4,4,6,53,2342,555,34,55};
    private int[] dest = {1,2,3,4,5,6,7,8,9,10};

    public void systemArrayCopy(){
        System.arraycopy(src,0,dest,3,3);
        System.out.print(Arrays.toString(dest));
    }
    public void arraysCopyOf(){
        int[] copyOf = Arrays.copyOf(src,src.length);
        System.out.println(Arrays.toString(copyOf));

    }
    public void arraysBinarySearch(){
        System.out.println(Arrays.binarySearch(src,1));
    }
    public void arrayBuildAndShow(){
        int[] arrayInt = new int[10];
        //流式初始化
        Stream.iterate(0,i->i=i+1).limit(10).forEach(element->arrayInt[element]=element+1);
        //流式输出
        Stream.iterate(0,i->i=i+1).limit(10).forEach(element->{
            if(element==0){
                System.out.print(arrayInt[element]);
            }else{
                System.out.print(","+arrayInt[element]);
            }
        });

        System.out.println();
        System.out.println("数组长度为："+arrayInt.length);
        System.out.println(arrayInt.hashCode());

        //lambda代表式的写法
        Arrays.stream(arrayInt).filter(value->value%2==0).forEach(value->System.out.print(value));
        //匿名内部类的写法
        Arrays.stream(arrayInt).filter(new IntPredicate() {
            public boolean test(int value) {
                return value%2==0;
            }
        }).forEach(new IntConsumer() {
            public void accept(int value) {
                System.out.print(value);
            }
        });
    }
}
